import Axios from "./axios.js";

export default class {
  rootPath = "";

  templates = {};

  readyFunction = Function.prototype;

  constructor(options) {
    this.rootPath = options.rootPath;
  }

  load(templates) {
    const requests = [];
    for (let i = 0; i < templates.length; i++) {
      requests.push(() => {
        return Axios.get(this.rootPath + templates[i]);
      });
    }
    Promise.all(
      requests.map(function (item) {
        return item();
      })
    ).then((results) => {
      for (let i = 0; i < templates.length; i++) {
        this.templates[templates[i]] = results[i].data;
      }
      this.readyFunction();
    });
    return this;
  }

  ready(callback) {
    this.readyFunction = callback;
  }

  get(template) {
    return this.templates[template];
  }
}
